<?php

namespace app\api\controller;

use app\admin\model\activity\Tuangou;
use app\admin\model\order\Spelldetail;
use app\admin\model\SpellOrder;
use app\common\controller\Api;
use fast\Http;
use fast\Utils;
use think\Db;
use think\Exception;

/**
 * 团购活动信息接口
 */
class Promotion extends Api
{
    protected $noNeedLogin = '*';
    protected $day;
    public function _initialize()
    {
        parent::_initialize();
        $this->day=\think\config::get("site.saleDays");;
    }

    public function index(){
        $type='android';
        $url= $url = \think\config::get('site.'.$type.'url');
        header('Location:'.$url);
    }

    public function getList(){
        $list=Tuangou::where(['status'=>['>',0]])->order('id desc')->select();
        foreach ($list as &$row){
            $row['pnum']=$row['pnum']>$row['zcount']?$row['zcount']:$row['pnum'];
        }
        $this->success('ok',$list);
    }

    public function getGoods(){
        $actid=$this->request->request('actid',0);
        $result=\app\admin\model\goods\Goods::all(['actid'=>$actid]);
        $this->success('ok',$result);
    }

    /**
     * 我的订单
     * @ApiMethod   (POST)
     * @ApiHeaders(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams(name="status", type="string", required=true, description="状态012345")
     * @ApiParams(name="page", type="string", required=true, description="分页")
     */
    public function orderList()
    {
        $model = new SpellOrder();
        $model::setRebate($this->auth->id);  //返佣检测
        $query = $model->where('uid', $this->auth->id);
        $status = $this->request->request("status",0);
        if($status == 1){
            $query->where("orderStatus",1);
        }elseif($status == 2){
            $query->where("orderStatus",2);
        }elseif($status == 3){
            $query->where("orderStatus",3);
        }elseif($status == 4){
            $query->where("orderStatus",'in',[4,5]);
        }elseif($status == 5){
            $query->where("orderStatus",'in',[6,7]);
        }elseif($status == 11){
            $query->where("orderStatus",11);
        }elseif($status == 12){
            $query->where("orderStatus",12);
        }
        $query->where('model',3);
        $list = $query->limit(($this->page-1)*$this->limit,$this->limit)
            ->field('id,uid,model,goodsName,skuName,thumb,goodsPrice,goodsNum,goodsId,payPrice,orderStatus,orderId,payType,actid,finishtime')
            ->order('id desc')
            ->limit(($this->page-1)*$this->limit,$this->limit)
            ->select();
        if(!empty($list)){
            foreach ($list as $ks=>&$vs){
                $vs['promInfo']=Tuangou::get(['id'=>$vs['actid']]);
                if(empty($vs['promInfo'])) continue;
                $vs['buyNum']=$vs['goodsNum'];
                $vs['profit']=bcmul($vs['payPrice'],$vs['promInfo']['rate']*0.01,2);
                if($vs['orderStatus']>=11){
                    $vs['saleTime']=$vs['promInfo']['finish_time']+$this->day*86400;
                    $difftime=time()-$vs['promInfo']['finish_time'];
                    $progress=$difftime>=$this->day*86400?100:bcdiv($difftime,$this->day*86400,2)*100;
                    $vs['saleProgress']=$progress;
                    $vs['isDelivery']=time()-$vs['promInfo']['finish_time']>86400?0:1;
                }
                $data[]=$vs;
            }
        }
        $data = array(
            "list"=>!empty($data)?$data:[],
            "orderStatus"=>[
                1=>$model->orderStatusInfo(1),
                2=>$model->orderStatusInfo(2),
                3=>$model->orderStatusInfo(3),
            ],
            "backPercent"=>bcadd(100, \think\config::get("site.pingorderbackpercent")),
        );
        $this->success("我的订单",$data);
    }

    /**
     * @Explain :拼购详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @Date : 2020/12/1 15:16
     * @Author : 河南邦耀网络科技有限公司
     */
    public function spellDetail(){
        $model = new SpellOrder();
        $actid=$this->request->request('actid',0);
        $query = $model->where('actid',$actid);
        $query->where("orderStatus",'in',[2,3,4,6,7,11,12]);
        $query->where('model',3);
        $query->where('uid',$this->auth->id);
        $list = $query->limit(($this->page-1)*$this->limit,$this->limit)
            ->field('id,uid,model,mobile,goodsName,skuName,thumb,goodsPrice,goodsNum,goodsId,payPrice,orderStatus,orderId,payType,actid,payTime')
            ->order('id desc')
            ->limit(($this->page-1)*$this->limit,$this->limit)
            ->select();
       $promInfo=Tuangou::get(['id'=>$actid]);
        if(!empty($list)){
            foreach ($list as $ks=>&$vs){
                $vs['buyNum']=$vs['goodsNum'];
                $vs['profit']=bcmul($vs['payPrice'],$promInfo['rate']*0.01,2);
                $vs['mobile']=substr($vs['mobile'], 0,3)."****".substr($vs['mobile'], 7,4);
            }
        }
        $promInfo['list'] =!empty($list)?$list:[];
        $promInfo['image']=(array) $promInfo['image'];
        $this->success("我的订单",$promInfo);
    }

}
